package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.WalletAssignService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;

@Controller
@RequestMapping("/walletAssign")

public class WalletAssignAction  implements Serializable{
	/** 日志  */
	private static final Log logger = LogFactory.getLog(WalletAssignAction.class);
	
	/** 服务service */
	@Autowired
	private WalletAssignService assignService;

	/** E400044 获取涨薪宝系列可转让债权列表
	 * @param request
	 * @return response
	 * */
	@RequestMapping(value = "/getAssignList")
	public void getAssignList(HttpServletRequest request, HttpServletResponse response){
		/** 默认分页参数 */
		Integer current = 1;
		Integer pageSize = 10;
		
		/** 密请求参数 */
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		
		/** 上送参数  */
		String userId = paramsMap.get("userId");   // 用户id
		String financeID = paramsMap.get("financeID");   // 产品id
		String orderCondition = paramsMap.get("orderCondition");   // 排序方法
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);   //当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);     //页大小
		
		/** 参数校验  */
		// 用户id校验
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
				Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
				Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/walletAssign/getAssignList 传入参数为空-----用户id");
			return;
		}
		// 理财产品id校验
		if (StringUtils.isBlank(financeID)) {
			financeID = this.assignService.getWalletFinanceString();
		}
		
		// 排序方法为空，取默认排序（id倒序）
		if (StringUtils.isBlank(orderCondition)) {
			orderCondition = "0";
		}
		// 分页设置
		if(currentStr != null && !("".equals(currentStr))){
			current = Integer.parseInt(currentStr);
		}
		if(pageSizeStr != null && !("".equals(pageSizeStr))){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			// 设置核心接口调用参数
			map.put("userId", userId);                       // 用户id
			map.put("financeID", financeID);             	 // 产品id
			map.put("orderCondition", orderCondition);       // 排序
			map.put(Consts.Page.PAGE_CURRENT_NAME, current); // 当前页
			map.put(Consts.Page.PAGE_SIZE_NAME, pageSize);   // 页大小
			
			// 获取涨薪宝系列下可转让债权列表
			Map<String, Object> jsonMap = assignService.getAssignList(map);
			
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.debug("/walletAssign/getAssignList 获取涨薪宝系列下可转让债权列表异常：" + e.getCause());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
				Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
				Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
	
	/** E400045 获取涨薪宝系列转让中债权列表
	 * @param request
	 * @return response
	 * */
	@RequestMapping(value = "/getInAssignList")
	public void getInAssignList(HttpServletRequest request, HttpServletResponse response){
		/** 默认分页参数 */
		Integer current = 1;
		Integer pageSize = 10;
		
		/** 密请求参数 */
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		
		/** 上送参数  */
		String userId = paramsMap.get("userId");   // 用户id
		String financeID = paramsMap.get("financeID");   // 产品id
		String orderCondition = paramsMap.get("orderCondition");   // 排序方法
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);   //当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);     //页大小
		
		/** 参数校验  */
		// 用户id为空
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
														Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
														Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/eplanAssign/getInAssignList 传入参数为空-----用户id");
			return;
		}
		
		// 传入理财产品id为空，取涨薪宝下所有产品
		if (StringUtils.isBlank(financeID)) {
			financeID = this.assignService.getWalletFinanceString();
		}
		
		// 排序方法为空，取默认排序（id倒序）
		if (StringUtils.isBlank(orderCondition)) {
			orderCondition = "0";
		}
		
		if(currentStr != null && !("".equals(currentStr))){
			current = Integer.parseInt(currentStr);
		}
		if(pageSizeStr != null && !("".equals(pageSizeStr))){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			// 设置核心接口调用参数
			map.put("userId", userId);                       // 用户id
			map.put("financeID", financeID);             	 // 产品id
			map.put("orderCondition", orderCondition);       // 排序
			map.put(Consts.Page.PAGE_CURRENT_NAME, current); // 当前页
			map.put(Consts.Page.PAGE_SIZE_NAME, pageSize);   // 页大小
			
			// 获取涨薪宝下转让中债权列表
			Map<String, Object> jsonMap = assignService.getInAssignList(map);
			
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			logger.debug("/eplanAssign/getInAssignList 获取涨薪宝下转让中债权列表 异常：" + e.getCause());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
														Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
														Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
	
	/** E400046 获取涨薪宝系列已转让债权列表
	 * @param request
	 * @return response
	 * */
	@RequestMapping(value = "/getAssignCompList")
	public void getAssignCompList(HttpServletRequest request, HttpServletResponse response){
		Integer current = 1;
		Integer pageSize = 10;
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		
		String userId = paramsMap.get("userId");   // 用户id
		String financeID = paramsMap.get("financeID");   // 产品id
		String orderCondition = paramsMap.get("orderCondition");   // 排序方法
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);   //当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);     //页大小
		
		// 用户id为空
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
														Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
														Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/eplanAssign/getAssignCompList 传入参数为空-----用户id");
			return;
		}
		
		// 传入理财产品id为空，取涨薪宝下所有产品
		if (StringUtils.isBlank(financeID)) {
			financeID = this.assignService.getWalletFinanceString();
		}
		
		// 排序方法为空，取默认排序（id倒序）
		if (StringUtils.isBlank(orderCondition)) {
			orderCondition = "0";
		}
		
		if(currentStr != null && !("".equals(currentStr))){
			current = Integer.parseInt(currentStr);
		}
		if(pageSizeStr != null && !("".equals(pageSizeStr))){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			// 设置核心接口调用参数
			map.put("userId", userId);                       // 用户id
			map.put("financeID", financeID);             	 // 产品id
			map.put("orderCondition", orderCondition);       // 排序
			map.put(Consts.Page.PAGE_CURRENT_NAME, current); // 当前页
			map.put(Consts.Page.PAGE_SIZE_NAME, pageSize);   // 页大小
			
			// 获取涨薪宝系列下已转让债权列表
			Map<String, Object> jsonMap = assignService.getAssignCompList(map);
			
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			logger.debug("/eplanAssign/getAssignCompList 获取涨薪宝系列下已转让债权列表 异常：" + e.getCause());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
														Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
														Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
	
	
	
}
